/**事件滑块2017-12 -27**/
(function(window, undefined) {
    "user strict";
    var vRange = function(params, callback) {
        this._init(params, callback)
    }

    var time = null;

    var distance, btnLeft, btnRight, innerBar ,toolTipWidth; //最早时间，最晚时间；
    var leftBtn, rightBtn;
    var minTime,maxTime;//当前时间和第二天0点时间
    var tLeftBubble,tRightBubble;//左边气泡和右边气泡
    var selectMinTime,selectMaxTime;
    var tTotalBarW;//bar容器的总宽度
    var tContainerLeft;//容器的left

    vRange.prototype = {
        _init: function(params, callback) {
            var self = this;
            var leftLis;//left:左滑
            var rightLis;//right
            var leftBtnClicked = false;//是否点击了左边按钮
            var rightBtnClicked = false;//是否点击rrrrrrr
            tContainerLeft = $api.offset(params.element).l;
            leftBtn = $api.dom('.slide-btn-left');
            rightBtn = $api.dom('.slide-btn-right');
            innerBar = $api.dom('.range-inner');
            tLeftBubble = $api.byId('leftBubble');
            tRightBubble = $api.byId('rightBubble');
            tTotalBarW = $api.offset($api.dom('.range-bars')).w;
            /***时间计算**/
            var tTime = new Date();
            minTime = tTime.getTime();
            selectMinTime = minTime;
            var tTime2 = new Date();
            tTime2.setHours(0,0,0,0);
            maxTime  = tTime2.getTime() + 1000*60*60*24;
            selectMaxTime = maxTime;
            distance = Math.abs(maxTime - minTime);
            callback(selectMinTime,selectMaxTime);
            /****/
            /**气泡初始化赋值**/
            var minHours = tTime.getHours();
            var minMins = tTime.getMinutes();
            var maxHours = tTime2.getHours();
            var maxMins = tTime2.getMinutes();
            $api.html(tLeftBubble,fix(minHours,2)+':'+fix(minMins,2));
            $api.html(tRightBubble,fix(maxHours,2)+':'+fix(maxMins,2));
            /****/
            btnLeft = $api.cssVal(leftBtn, 'left');
            btnRight = $api.cssVal(rightBtn, 'right');
            toolTipWidth = params.element.offsetWidth;
            $api.addEvt(leftBtn, 'touchstart', function(e) {
              e.preventDefault();
              e.stopPropagation();
              leftBtnClicked = true;
            });
            $api.addEvt(rightBtn, 'touchstart', function(e) {
              e.preventDefault();
              e.stopPropagation();
              rightBtnClicked = true;
            });
            $api.addEvt(leftBtn, 'touchmove', function(e) {
                e.preventDefault();
                e.stopPropagation();
                if(leftBtnClicked){
                  var tTouchX = e.touches[0].pageX || e.touches[0].clientX;
                  var tInnerBarW = $api.offset(innerBar).w;
                  var tInnerLeft = $api.offset(innerBar).l;
                  var tInnerCssLeft = tInnerLeft - tContainerLeft;
                  // console.log(tInnerBarW+'---------------'+tInnerLeft)
                  var tMoveX = tTouchX - tContainerLeft;
                  // console.log(tMoveX)
                  if(  tInnerBarW > 20  && tMoveX > 0){
                      var tW =  tInnerBarW - (tTouchX - tInnerLeft);
                      var tL = tMoveX;
                      $api.css(innerBar,'width:'+ tW +'px;left:'+ tL +'px');
                      self._getCurrrentTime();
                      callback(selectMinTime,selectMaxTime)
                      if($api.offset(innerBar).w < 30 ){//如果超过边界，恢复上次的位置
                        $api.css(innerBar,'width:'+ tInnerBarW +'px;left:'+ tInnerLeft +'px');
                        self._getCurrrentTime()
                        callback(selectMinTime,selectMaxTime)
                      }
                  }
                }
            });
            $api.addEvt(rightBtn, 'touchmove', function(e) {
                e.preventDefault();
                e.stopPropagation();
                if(rightBtnClicked){
                  var tTouchX = e.touches[0].pageX || e.touches[0].clientX;
                  var tInnerBarW = $api.offset(innerBar).w;
                  var tInnerLeft = $api.offset(innerBar).l;
                  var tInnerCssLeft = tInnerLeft - tContainerLeft;
                  // console.log(tInnerBarW+'---------------'+tInnerLeft)
                  var tMoveX = tTouchX - tInnerLeft - tInnerBarW;
                  // console.log(tMoveX)
                  if(  tInnerBarW > 20 ){
                      var tW =  tInnerBarW +  tMoveX;
                      $api.css(innerBar,'width:'+ tW +'px;');
                      self._getCurrrentTime();
                      callback(selectMinTime,selectMaxTime)
                      if($api.offset(innerBar).w < 30 ){//如果超过边界，恢复上次的位置
                        $api.css(innerBar,'width:'+ tInnerBarW +'px;');
                      self._getCurrrentTime();
                      callback(selectMinTime,selectMaxTime)
                    }else if(($api.offset(innerBar).l + $api.offset(innerBar).w)>tTotalBarW + tContainerLeft){//右侧不能超出边界
                      $api.css(innerBar,'width:'+ tInnerBarW +'px;');
                      self._getCurrrentTime();
                    }
                  }
                }
            });
            $api.addEvt(leftBtn, 'touchend',function(e){
              e.preventDefault();
              e.stopPropagation();
              leftBtnClicked = false;
            });
            $api.addEvt(rightBtn, 'touchend',function(e){
              e.preventDefault();
              e.stopPropagation();
              rightBtnClicked = false;
            });
        },
        _getCurrrentTime:function(){
          var tInnerBar = $api.dom('.range-inner');
          var leftBtn = $api.dom('.slide-btn-left');
          var rightBtn = $api.dom('.slide-btn-right');
          var tInnerBarL = $api.offset(tInnerBar).l;
          var tInnerBarW = $api.offset(tInnerBar).w;
          var tLeftTime = minTime + distance * ((tInnerBarL-tContainerLeft)/tTotalBarW);//时间错
          var tRightTime = minTime + distance * ((tInnerBarL+ tInnerBarW - tContainerLeft)/tTotalBarW);
          var tLeftTime1 = new Date(tLeftTime);
          var tLeftHours = tLeftTime1.getHours();
          var tLeftMins = tLeftTime1.getMinutes();
          var tRightTime1 = new Date(tRightTime);
          var tRihgtHours = tRightTime1.getHours();
          var tRightMins = tRightTime1.getMinutes();
          selectMinTime = (tLeftTime.toString()).split('.')[0];
          selectMaxTime = (tRightTime.toString()).split('.')[0];
          $api.html(tLeftBubble,fix(tLeftHours,2)+':'+fix(tLeftMins,2));
          $api.html(tRightBubble,fix(tRihgtHours,2)+':'+fix(tRightMins,2));
        },
        _setCurrrentTime:function(pStartTime,pEndTime){

        },
        _reSetTime:function(callback){
           var tMinTimeStamp = minTime.getTime();
           var tMaxTimeStamp = maxTime.getTime();
           callback(tMinTimeStamp,tMaxTimeStamp)

        }

    }

    window.vRange = vRange;
})(window)
//日期加0
function fix(num, length) {
    return ('' + num).length < length ? ((new Array(length + 1)).join('0') + num).slice(-length) : '' + num;
}
